<?php
    namespace app\client\lib;
    class ChargeHttp {
        private $client = null;
        private $host = ['106.14.135.226'];
        private $port = 7777;
        
        public function __construct($gatewayNo) {
            $this->client = new \Swoole\Coroutine\Http\Client($this->host[$gatewayNo - 1], $this->port);
            $this->client->setHeaders(['Content-Type' => 'application/json']);
            $this->client->set(['timeout' => 60]);
            
           
            
            
        }
        
        public function send($url, $data) {
            if(is_array($data)) {
                $data = json_encode($data);
            }
            
            $repResult = [
                'success' => false,
                'body' => ''
            ];
            
            $this->client->post('/sendGateway/' .  $url, $data);
            $repResult['body'] = $this->client->body;
            
            $result = json_decode($repResult['body'], true);
            
            if($result !== null){
                if(isset($result['statuscode']) && $result['statuscode'] == 200){
                    $repResult['success'] = true;
                }
            }
            
            return $repResult;
            
            
        }
    }